﻿using Microsoft.Extensions.Logging;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using WebViewControl.Contracts;
using WebViewControl.Services;
using WebViewModule.Services;

namespace WebViewControl
{
    public class WebviewViewModel  : BindableBase, INotifyPropertyChanged, INavigationAware
    {
         
        public event PropertyChangedEventHandler PropertyChanged; 
        private async void click()
        {
            await _webViewManager.NavigateAsync(WebViewName, "https://www.baidu.com");
        }
        protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
        {
            if (!(object.Equals(field, newValue)))
            {
                field = (newValue);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
                return true;
            }

            return false;
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
        
        }

        public bool IsNavigationTarget(NavigationContext context) => true;
        public void OnNavigatedFrom(NavigationContext context) { }

        public DelegateCommand  ClickCommand { get; set; }

        #region 
        private  string  WebViewName =""; 
        private readonly IWebViewManager _webViewManager;
         
        public WebviewViewModel(IWebViewManager webViewManager)
        {
            _webViewManager = webViewManager; 
            this.ClickCommand = new DelegateCommand(click); 
        }

        public void SET_WEBViewNAME(string name)
        {
            this.WebViewName = name;
    }

        #endregion
    }

}